package com.dhcc.sds.base.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.service.ICdcEpisodeService;
import com.dhcc.cdc.task.CdcAutoTask;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.service.ISdsFillByDoctorService;
import com.dhcc.sds.task.QcTask;
import com.dhcc.sds.task.QzjDataSyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${sys.backendPath}/sds/common")
public class SdsCommonController extends BaseController {
    private String PREFIX = "/backend/sds/manage/";

    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private QcTask qcTask;
    @Autowired
    private ICdcEpisodeService cdcEpisodeService;
    @Autowired
    private ISdsFillByDoctorService sdsFillByDoctorService;
    @Autowired
    private QzjDataSyncTask qzjDataSyncTask;
    @Autowired
    private CdcAutoTask cdcAutoTask;

    /**
     * 跳转到手动入组筛查页面
     */
    @RequestMapping("/manual")
    public String manual() {
        return PREFIX + "manualEval.html";
    }

    /**
     * 跳转到自动取值手动操作页面
     */
    @RequestMapping("/manualValue")
    public String manualValue(Model model) {
        return PREFIX + "manualValue.html";
    }

    /**
     * 跳转到自动取值手动操作页面
     */
    @RequestMapping("/synQzj")
    public String synQzj(Model model) {
        return PREFIX + "synQzj.html";
    }

    /**
     * 手动执行入组判定任务
     * @return
     */
    @RequestMapping("/manualMrList")
    @ResponseBody
    public Object manualMrList(@RequestParam Map<String, Object> params){
        try {
            qcTask.manualMrList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 跳转到手动入组筛查页面
     */
    @RequestMapping("/manualCus")
    public String manualCus() {
        return PREFIX + "manualCusEval.html";
    }

    /**
     * 手动执行自定义病种入组判定任务
     * @return
     */
    @RequestMapping("/manualCusMrList")
    @ResponseBody
    public Object manualCusMrList(@RequestParam Map<String, Object> params){
        try {
            qcTask.manualCusMrList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }


    /**
     * 手动取值操作
     * @return
     */
    @RequestMapping("/manualInitMrList")
    @ResponseBody
    public Object manualInitMrList(@RequestParam Map<String, Object> params){
        try {
            qcTask.manualInitMrList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 手动计算填充率
     * @return
     */
    @RequestMapping("/manualFillRate")
    @ResponseBody
    public Object manualFillRate(@RequestParam Map<String, Object> params){
        try {
            qcTask.manualFormFillRate(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 手动同步前置机信息
     * @return
     */
    @RequestMapping("/manualSynPatientInfo")
    @ResponseBody
    public Object manualSynPatientInfo(@RequestParam Map<String, Object> params){
        try {
            qzjDataSyncTask.SynPatientInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 执行手动评估进度
     * @return
     */
    @RequestMapping("/getProcess")
    @ResponseBody
    public Object getProcess() {
        return R.ok(qcTask.getProcess());
    }

    /**
     * 执行手动评估进度
     * @return
     */
    @RequestMapping("/getQzjProcess")
    @ResponseBody
    public Object getQzjProcess() {
        return R.ok(qzjDataSyncTask.getProcess());
    }

    /**
     * 跳转到测试上报病例页面
     */
    @RequestMapping("/testPatient")
    public String testPatient(Model model) {
        return PREFIX + "testPatient.html";
    }

    /**
     * 获取病种筛查配置列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/testPatientList")
    @ResponseBody
    public Object testPatientList(@RequestParam Map<String, Object> params) {
        params.put("testFlg", 1);
        Page<SdsQcincaseEntity> page = new PageFactory<SdsQcincaseEntity>().defaultPage();
        List<SdsQcincaseEntity> result = this.sdsFillByDoctorService.page(page,params);
        page.setRecords((List<SdsQcincaseEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取病种筛查配置列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/entityTestGroup")
    @ResponseBody
    public Object entityTestGroup() {
         return this.sdsCommonService.entityTestGroup();
    }

    /**
     * 获取科室列表
     * @return
     */
    @RequestMapping("/deptListByHospId")
    @ResponseBody
    public Object deptListByHos(@RequestParam Map<String, Object> params){
        return this.sdsCommonService.getDepartMap(params);
    }

    /**
     * 获取医疗组列表
     * @return
     */
    @RequestMapping("/groupListByDeptId")
    @ResponseBody
    public Object groupListByDeptId(@RequestParam Map<String, Object> params){
        return this.sdsCommonService.getMedicalGroupMap(params);
    }

    /**
     * 获取字典列表
     * @return
     */
    @RequestMapping("/dicListByTypeCode/{typeCode}")
    @ResponseBody
    public Object dicListByTypeCode(@PathVariable String typeCode){
        return this.sdsCommonService.getDicArray(typeCode);
    }

    /**
     * 获取病种列表
     * @return
     */
    @RequestMapping("/entityList")
    @ResponseBody
    public Object entityList(){
        return this.sdsCommonService.getEntityMap("Up");
    }

    /**
     * 获取自定义病种列表
     * @return
     */
    @RequestMapping("/entityCusList")
    @ResponseBody
    public Object entityCusList(){
        return this.sdsCommonService.getEntityMap("Custom");
    }

    /**
     * 跳转到生成门户数据页面
     */
    @RequestMapping("/initPortalData")
    public String initPortalData() {
        return PREFIX + "initPortalData.html";
    }

    /**
     * 手动执行生成门户数任务
     * @return
     */
    @RequestMapping("/initPortalDataMrList")
    @ResponseBody
    public Object initPortalDataMrList(@RequestParam Map<String, Object> params){
        try {
            qcTask.initPortalDataMrList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 手动执行解析病历数据
     * @return
     */
    @RequestMapping("/analysisEmrData")
    @ResponseBody
    public Object analysisEmrData(@RequestParam Map<String, Object> params){
        try {
            cdcAutoTask.analysisEmrData(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 手动执行解析检查数据
     * @return
     */
    @RequestMapping("/analysisExamData")
    @ResponseBody
    public Object analysisExamData(@RequestParam Map<String, Object> params){
        try {
            cdcAutoTask.analysisExamData(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @RequestMapping("/getEmrProcess")
    @ResponseBody
    public Object getEmrProcess() {
        return R.ok(cdcAutoTask.getProcess());
    }

}
